package link.jfire.socket.socketserver.interceptor.impl;

import link.jfire.baseutil.simplelog.ConsoleLogFactory;
import link.jfire.baseutil.simplelog.Logger;
import link.jfire.socket.socketserver.bus.BusinessCommand;
import link.jfire.socket.socketserver.interceptor.EntryInterceptor;
import link.jfire.socket.socketserver.storage.Entry;

public class AuthInterceptor implements EntryInterceptor
{
    private Logger logger = ConsoleLogFactory.getLogger();
    
    @Override
    public int getOrder()
    {
        return Integer.MIN_VALUE;
    }
    
    @Override
    public int inInterceptor(Entry entry)
    {
        if (entry.getServerChannelInfo().isAuthentication())
        {
            return EntryInterceptor.next;
        }
        else
        {
            if (entry.getCommand() == BusinessCommand.AUTH || entry.getCommand() == BusinessCommand.SENDKEY)
            {
                return EntryInterceptor.end;
            }
            else
            {
                logger.warn("未授权异常线路，地址为{}。关闭该线路。", entry.getServerChannelInfo().getAddress());
                entry.getServerChannelInfo().close();
                return EntryInterceptor.interceptored;
            }
        }
    }
    
    @Override
    public int outInterceptor(Entry entry)
    {
        return EntryInterceptor.end;
    }
    
}
